package com.powernode.controller;

import com.github.pagehelper.PageInfo;
import com.powernode.common.Result;
import com.powernode.config.UploadProperties;
import com.powernode.config.UploadService;
import com.powernode.domain.Attachment;
import com.powernode.service.AttachmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
 * 内容:
 * 作者: cy
 */
@RestController
@RequestMapping("attachment")
public class AttachmentController {

    @Autowired
    private AttachmentService attachmentService;

    @Autowired
    private UploadService uploadService;

    @Autowired
    private UploadProperties properties;


    @RequestMapping("doUpload")
    public Result doUpload(MultipartFile mf){
        List<Map<String,Object>> data=new ArrayList<>();
        try {
            //保存到数据库
            Attachment attachment=new Attachment();
            attachment.setName(mf.getOriginalFilename());
            attachment.setCreateTime(new Date());
            attachment.setSize(mf.getSize()+0.0);
            attachment.setType(mf.getContentType());
            Map<String, Object> res = this.uploadService.uploadFile(mf);
            data.add(res);
            //取出相对路径
            String relativePath = res.get("relativePath").toString();
            attachment.setPath(relativePath);
            this.attachmentService.insertSelective(attachment);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,"上传失败",e.getMessage());
        }
        return new Result(200,"上传成功",data);
    }


    @RequestMapping("loadAllAttachmentForPage")
    public Result loadAllAttachmentForPage(Integer page,Integer limit){
        PageInfo<Attachment> pageInfo=this.attachmentService.queryAttachmentForPage(page,limit);
        pageInfo.getList().forEach(attachment -> attachment.setPath(properties.getBaseUrl()+attachment.getPath()));
        return new Result(0,"查询成功",pageInfo.getTotal(),pageInfo.getList());
    }


    /**
     * 根据ID删除文件
     */
    @PostMapping("delAttachment")
    public Result delAttachment(Integer id){
        try {
            //根据ID查询文件对象取出来路径
            Attachment attachment = this.attachmentService.selectByPrimaryKey(id);
            if(null!=attachment){
                this.uploadService.deleteFile(attachment.getPath());//把文件服务器的数据删除了
                //删除数据库里面的数据
                this.attachmentService.deleteByPrimaryKey(id);
                return new Result(200,"删除成功");
            }else{
                return new Result(-1,"删除失败","该文件在数据库中不存在");
            }

        }catch (Exception e){
            e.printStackTrace();
            return new Result(-1,"删除失败",e.getMessage());
        }
    }


}
